package character.action;

import gameobject.construct.Construct;
import character.AbstractCharacter;
import gameobject.item.RequiredItemHolder;
import mygame.DefaultValues;
import mygame.VoxelGrid;

public class HarvestAction extends CharacterAction {

    public HarvestAction() {}

    public HarvestAction(Construct targetConstruct) {
	this.targetConstruct = targetConstruct;
    }

    public Construct getConstructToDig() {
	return targetConstruct;
    }

    public void setConstructToDig(Construct targetConstruct) {
	this.targetConstruct = targetConstruct;
    }

    @Override
    protected boolean execute(AbstractCharacter performer) {
        if(!targetConstruct.isBuilt()) { //if it's already been harvested
            return true;
        } else {
            float distanceToTarget = ActionUtility.getDistCharToConstruct(performer, targetConstruct);
            if(distanceToTarget < DefaultValues.ACTION_INTERACT_DISTANCE) {
		if(checkRequiredItemsToHarvest(performer)) {
		    targetConstruct.modifyHitPoints(performer.getSettings().getHarvestSpeed());
		    System.out.println("Harvesting "+targetConstruct+"... "+targetConstruct.getHitPoints());
		    if(!targetConstruct.isBuilt()) {
			performer.getInventory().addItems(targetConstruct.getInventory().popAllItems());
			return true;
		    }
		} else {
		    System.out.println("Lacking required items to harvest "+targetConstruct);
		}
            } else {
                performer.queueActionFront(new MoveAction(VoxelGrid.getWorldLocation(targetConstruct.getBaseVoxelGridLocation()), DefaultValues.ACTION_INTERACT_DISTANCE));
            }
        }
	return false;
    }

    protected boolean checkRequiredItemsToHarvest(AbstractCharacter performer) {
	if(performer.getInventory().containsRequiredItemsWithQuantities(targetConstruct.getRequiredItemsToHarvest(), RequiredItemHolder.CHARACTER)
		&& targetConstruct.getInventory().containsRequiredItemsWithQuantities(targetConstruct.getRequiredItemsToHarvest(), RequiredItemHolder.CONSTRUCT))
	    return true;
	else
	    return false;
    }
}
